#include <QPaintEvent>
#include <QPainter>
#include <cmath>

#include "space.h"
#include "world.h"
#include "particle.h"

Space::Space(QWidget *parent, World * world_) : QWidget(parent) {
    world = world_;
    setPalette(QPalette(QColor(250, 250, 200)));
    setAutoFillBackground(true);
}


void Space::paintEvent(QPaintEvent *) {
    Particle * tmp;
    QPainter painter(this);
    int dim;
    //painter.setPen(QColor(0, 0, 0));
    
    for (int i = 0; i<= world -> numberOfParticles() - 1; i++) {
        tmp = world -> getParticle(i);
        if (tmp -> get_charge() < 0) {
            painter.setBrush(QBrush(QColor(255, 0, 0)));
        } else if (tmp -> get_charge() > 0) {
            painter.setBrush(QBrush(QColor(0, 0, 255)));
        } else {
            painter.setBrush(QBrush(QColor(0, 0, 0)));
        }
        dim = fabs(tmp -> get_charge()) / 10;
        painter.fillRect(tmp -> get_x(), tmp -> get_y(),
                            dim, dim, painter.brush());
    }
}
